package org.jeecg.modules.wangyue.utils;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 *  文件工具类
 */
@Slf4j
public class FileUtil {


    /**
     * 上传新文件,删除老文件
     * @param file      上传的文件
     * @param oldUrl    老文件的url
     * @param addPath   上传文件保存路径
     * @return
     */
    public static String uploadNewAndDelOld(MultipartFile file, String oldUrl, String addPath) {
        String newUrl = null;
        try{
            //如果不为空，则删除旧文件
            if(oConvertUtils.isNotEmpty(oldUrl)){
                FileUtil.delFile(oldUrl);
            }
            //上传文件
            newUrl = FileUtil.uploadFile(file, addPath);
        }catch (Exception e){
            e.printStackTrace();
        }
        return newUrl;
    }


    /**
     * 上传文件
     * @param mf 上传的文件
     * @param folderPath 保存的文件夹目录
     * @return
     */
    public static String uploadFile(MultipartFile mf, String folderPath){
        try {
//            String uploadPath = getUploadPath();
            String uploadPath = System.getProperty("user.dir"); //jar包路径
            File folder = new File(uploadPath + folderPath);
            System.out.println("uploadPath:" + uploadPath);
            System.out.println("folder:" + folder);
            if (!folder.exists()) {
                folder.mkdirs();// 创建文件夹
            }
            String fileName = CommonUtils.getFileName(mf.getOriginalFilename()); // 获取文件名
            if(fileName.indexOf(".") != -1){
                fileName = fileName.substring(0, fileName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
            }else{
                fileName = fileName+ "_" + System.currentTimeMillis();
            }
            String filePath = folder.getPath() + File.separator + fileName;
            File file = new File(filePath);
            System.out.println("上传文件路径：" + file);
            FileCopyUtils.copy(mf.getBytes(), file); //上传文件
            String url = folderPath + File.separator + fileName;
            if (url.contains("\\")) {
                url = url.replace("\\", "/");
            }
            return url;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }


    /**
     * 删除文件
     * @param url 文件的
     * @return
     */
    public static boolean delFile(String url){
//        String uploadPath = getUploadPath();
        String uploadPath = System.getProperty("user.dir"); //jar包路径
        String filePath = uploadPath + File.separator + url;
        return cn.hutool.core.io.FileUtil.del(filePath);
    }


    /**
     * 获取存放路径
     * @return
     */
    public static String getUploadPath(){
        String os = System.getProperty("os.name"); //获取操作系统
        System.out.println(os);
        if (os != null && os.toLowerCase().startsWith("windows")) { //Windows操作系统
            return cutPath(FileUtil.class.getClassLoader().getResource("").getPath());
        } else if (os != null && os.toLowerCase().startsWith("linux")) { //Linux操作系统
            return cutPath(FileUtil.class.getClassLoader().getResource("/").getPath());
        } else { //其它操作系统
            return cutPath(FileUtil.class.getClassLoader().getResource("").getPath());
        }
    }


    /**
     * 截取路径
     * @param path
     * @return
     */
    public static String cutPath(String path){
        System.out.println("上传路径前："+path);
        path=path.replace("file:","");
        path=path.replace(" ","");
        path=path.replace("%20"," ");
        int num=3;
        if (path.indexOf(".jar")!=-1){
            num++;
        }
        for (int i = 0; i < num; i++) {
            path=path.substring(0,path.lastIndexOf("/"));
        }
        System.out.println("上传路径后："+path);
        return path;
    }




    public static void main(String[] args) {
        System.out.println(getUploadPath());
    }


}
